package com.srwl.mytx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.services.core.PoiItem;
import com.srwl.mytx.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by dahai2070 on 2017/3/1 0001.
 */

public class AddressSearchResultAdapter extends RecyclerView.Adapter<AddressSearchResultAdapter.ItemViewHolder> {
    private Context mContext;
    private List<PoiItem> poiItems;
    private OnItemClickListener itemClickListener;

    public AddressSearchResultAdapter(Context mContext, List<PoiItem> dialogItems) {
        super();
        this.mContext = mContext;
        this.poiItems = dialogItems;
    }

    @Override
    public int getItemCount() {
        return poiItems.size();
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.address_search_result_item, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {

        PoiItem poiItem = poiItems.get(position);
        holder.tvTitle.setText(poiItem.getTitle());
        int distance = poiItem.getDistance();
        if (distance != -1) {
            holder.tvDistance.setText(distance + " | " + poiItem.getSnippet());
        } else {
            holder.tvDistance.setText(poiItem.getSnippet());
        }

        holder.itemView.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onClick(poiItem);
            }
        });

    }


    class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle;
        TextView tvDistance;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvDistance = itemView.findViewById(R.id.tv_distance);
        }
    }

    public interface OnItemClickListener {
        void onClick(PoiItem poiItem);
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

}
